package com.jdrx.sw.sewage.service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jdrx.platform.jdbc.beans.vo.PageVO;
import com.jdrx.sw.sewage.beans.dto.log.AddLogDTO;
import com.jdrx.sw.sewage.beans.dto.log.QueryLogDTO;
import com.jdrx.sw.sewage.beans.entity.LogPO;
import com.jdrx.sw.sewage.beans.vo.OptionVO;
import com.jdrx.sw.sewage.dao.LogDAO;

@Service
public class LogService {
	@Autowired
	private LogDAO logDAO;
	
	public void add(String tenant, String station, String user, String event, String terminal, String description) {
		LogPO logPO = new LogPO();
		logPO.setTenant(tenant);
		logPO.setStation(station);
		logPO.setUser(user);
		logPO.setEvent(event);
		logPO.setTerminal(terminal);
		logPO.setDescription(description);
		logDAO.insert(logPO);
	}
	
	public void add(AddLogDTO dto) {
		LogPO logPO = new LogPO();
		BeanUtils.copyProperties(dto, logPO);
		logDAO.insert(logPO);
	}
	
	public PageVO<LogPO> pageList(QueryLogDTO dto) {
		if(dto.getStartDate() != null) {
			dto.setStartDateTime(LocalDateTime.of(dto.getStartDate(), LocalTime.of(0, 0, 0)));
		}
		if(dto.getEndDate() != null) {
			dto.setEndDateTime(LocalDateTime.of(dto.getEndDate(), LocalTime.of(23, 59, 59)));
		}
		LogPO logPO = new LogPO();
		BeanUtils.copyProperties(dto, logPO);
		
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), "id desc");
		Page<LogPO> page = (Page<LogPO>) logDAO.list(logPO);
		return new PageVO<>(page);
	}
	
	public List<OptionVO> tenantOptions() {
		return logDAO.tenantOptions();
	}
	
	public List<OptionVO> stationOptions() {
		return logDAO.stationOptions();
	}
	
	public List<OptionVO> userOptions() {
		return logDAO.userOptions();
	}
	
	public List<OptionVO> eventOptions() {
		return logDAO.eventOptions();
	}
}